package com.pdf.pdfserver.controller;

import com.pdf.pdfserver.enums.FileSuffixEnum;
import com.pdf.pdfserver.service.PdfService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author songfz
 * @Description
 * @Date 2022/1/4
 */
@RestController
@RequestMapping("api/pdf")
public class PdfController {


    private static Logger LOG = LoggerFactory.getLogger(PdfController.class);

    private static Map<FileSuffixEnum, List<File>> map = new HashMap<>();

    @Autowired
    private PdfService pdfService;

    @PostMapping("create")
    public Boolean toPdf(String fromPath, String toPath) {

        map = rootFile(new File(fromPath));
        if(CollectionUtils.isEmpty(map)){
            return false;
        }
        try {
            pdfService.toPdf(map, toPath);
//            pdfService.officeToPDF(map,toPath);

            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }finally {
            map.clear();
        }
        return false;
    }


    /**
     * 获取根文件
     *
     * @param file
     * @return
     */
    private Map<FileSuffixEnum, List<File>> rootFile(File file) {


        if (file.isDirectory()) {
            if (!file.exists()) {
                file.mkdirs();
            }
            for (File file1 : file.listFiles()) {
                rootFile(file1);
            }
        } else if(file.exists()){
            FileSuffixEnum suffixEnum = FileSuffixEnum.getSuffix(file.getName());
            List<File> fileList = map.get(suffixEnum);
            if (CollectionUtils.isEmpty(fileList)) {
                List<File> files = new ArrayList<>();
                files.add(file);
                map.put(suffixEnum, files);
            } else {
                fileList.add(file);
                map.put(suffixEnum, fileList);
            }

        }
        return map;
    }


    /**
     *  PDF插入图片
     * @param inPath PDF源文件
     * @param outPath PDF输出地址
     * @param image 插入的图片地址
     * @param pageIndex 插入的页数
     * @return
     */
    @GetMapping("insert/pic")
    public Boolean insertPic(String inPath,String outPath,String image,Integer  pageIndex){

        return pdfService.insertPic(inPath,outPath,image,pageIndex);
    }


    /**
     * PDF文档页面大小统计
     * @param inPath pdf文档地址
     * @return
     */
    @GetMapping("total/page")
    public Integer totalPage(String inPath){

        return pdfService.totalPage(inPath);
    }

    /**
     * PDF文档读取指定页面图片
     * @param pdfPath pdf读取地址
     * @param imagePath 图片输出地址
     * @param pageIndex 页数
     */
    @GetMapping("read/pic")
    public  void readPic(String pdfPath, String imagePath, Integer pageIndex){
        pdfService.readPic(pdfPath,imagePath,pageIndex);
    }
}
